import pygame
import random
import os
import sys
import time
import numpy as np
import torch

from Controller import Director


class PPODirector(Director):
    def __init__(self, set):
        super().__init__(set)
        self.t = 0
        self.r = 0
        self.R = []
        self.actions = []
        self.count = np.zeros([self.set.height, self.set.width])
        self.max_cellcnt = self.set.width * self.set.height - self.set.mine_count

    def reset(self, action):
        self.t = 0
        self.r = 0
        self.R = []
        self.actions = []
        self.count = np.zeros([self.set.height, self.set.width])

        self.start(action)



    def getStatus(self):
        """
        !!!!!!!!!!!!!!!!尚未完成！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！
        :return:
        """
        status = np.zero([self.set.height, self.set.width])
        for x in range(self.set.height):
            for y in range(self.set.width):
                if self.reporter.isClicked(x,y):
                    status[x,y] = self.monitor.getNum(x, y)
                else:
                    status[x,y] = -1
        # 将点击次数矩阵与status合并
        status = np.stack((status, self.count), axis=0)
        return status

    def updateAction(self,action):
        [x, y] = action
        self.r = 0.
        if not self.reporter.isClickable(x, y):
            self.r = 0.
        elif not self.takeAction(x, y):
            self.r = 0.
        else:
            self.r = 1.0
        self.count[x, y] += 1
        d = not self.isEnd()

        if self.referee.count <= self.max_cellcnt and self.referee.count >= self.max_cellcnt - 10:
            self.r = 50.
            d = False

        s_t = torch.tensor(self.getStatus(),dtype=torch.float32)
        self.R.append(self.r)
        self.actions.append([x,y])

        self.t += 1

        if self.t == 50:
            self.r = 0.
            d = False

        return [s_t,self.r,d]

